<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-10-8
 * 版    本：1.0.0
 * 功能说明：商品类别控制器。
 *
 **/

namespace Qwadmin\Controller;


class GoodsCategoryController extends ComController{

    /*
     * 商品分类列表
     * */
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('goods_type');
        $pagesize = 10;#每页数量
        $count = $m->count();
        $list = $m->select();
        $list = $this->getMenu($list);
//        $page = new \Think\Page($count, $pagesize);
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 添加商品分类
     * */
    public function add()
    {
        $option = M('goods_type')->select();
        $option = $this->getMenu($option);
        $this->assign('option', $option);
        $this->display('form');
    }

    /*
    * 删除商品分类
    * */
    public function del()
    {
        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        if (is_array($ids)) {  //删除多个
            foreach ($ids as $k => $v) {
                //当前分类下有之子类的禁止删除
                if (M('goods_type')->where(array('pid' => intval($v)))->select()) {
                    $this->error('当前分类下还有子类不能删除！');
                    $ids[$k] = intval($v);
                }
                //当前分类下有商品的禁止删除
                if (M('goods')->where(array('goods_type_id' => intval($v)))->select()) {
                    $this->error('当前分类下有商品不能删除！');
                }
            }
        }elseif($ids){  //删除单个
            //当前分类下有之子类的禁止删除
            if (M('goods_type')->where(array('pid' => intval($ids)))->select()) {
                $this->error('当前分类下还有子类不能删除！');
            }
            //当前分类下有商品的禁止删除
            if (M('goods')->where(array('goods_type_id' => intval($ids)))->select()) {
                $this->error('当前分类下有商品不能删除！');
            }
        }else{
            $this->error('请选择类别');
        }
            $map['id'] = array('in', $ids);
            if (M('goods_type')->where($map)->delete()) {
                addlog('删除分类ID：' . $ids);
                $this->success('恭喜，分类删除成功！');
            } else {
                $this->error('参数错误！');
            }

    }

    public function edit($id = 0)
    {
        $id = intval($id);
        $m = M('goods_type');
        $currentmenu = $m->where("id='$id'")->find();
        if (!$currentmenu) {
            $this->error('参数错误！');
        }

        $option = $m->select();
        $option = $this->getMenu($option);
        $this->assign('option', $option);
        $this->assign('currentmenu', $currentmenu);
        $this->display('form');
    }

    public function update()
    {
        $m = M('goods_type');
        $id = I('post.id', '', 'intval');
        $data['pid'] = I('post.pid', '', 'intval');
        $data['name'] = I('post.name', '', 'strip_tags');
        $data['demo'] = I('post.demo');
        $data['commission'] = I('post.commission'); //分类佣金
        if($data['pid']==0){ //深度
            $data['depth'] = 1;
        }else{
            $pid =  $data['pid'];
            $i = 1;
             while($pid){
                 $pid = $m->where(array('id'=>$pid))->getField('pid');
                 $i++;
             }
            $data['depth'] = $i;
        }
        if ($id) {
            M('goods_type')->data($data)->where("id='{$id}'")->save();
            addlog('编辑分类，ID：' . $id);
        } else {
            M('goods_type')->data($data)->add();
            addlog('新增分类，名称：' . $data['name']);
        }

        $this->success('操作成功！','index');
    }
    /**
     * 商品属性
     */
    public function attributeList(){
        $category_id = I('get.id');
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = 20; #每页数量
        $count = M('attribute')->where(array('category_id'=>$category_id))->count();
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $list = M('attribute')
                ->alias('a')
                ->join('LEFT JOIN zt_goods_type as g ON g.id = a.category_id')
                ->where(array('a.category_id'=>$category_id))->limit($offset . ',' . $pagesize)
                ->field('a.*,g.name as category_name')
                ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('category_id',$category_id);
        $this->assign('lists',$list);
        $this->assign('page',$page);
        $this->display();
    }
    
    
    /*
     * 属性添加
     */
    public function attributeAdd(){
         $category_id = I('get.category_id');
        
        $m = M('attribute');
        if(IS_POST){
            $data = I('post.');
            $data['bute_val'] = str_replace("\r\n",',',$data['bute_val']);
            $data['create_time'] = time();
            if($m->add($data)){
                $this->success('恭喜，属性添加成功！',U('attributeList',array('id'=>$data['category_id'])));
            } else {
                $this->error('添加失败！');
            }
        }else{
            $option = M('goods_type')->select();
            $option = $this->getMenu($option);
            $currentmenu['category_id'] = $category_id;
            $this->assign('currentmenu',$currentmenu);
            $this->assign('option',$option);
            $this->display('attributeForm');
        }
        
    }
    
    
      /*
     * 属性编辑
     */
    public function attributeEdit(){
         $id = I('get.id');
         $m = M('attribute');
        
        if(IS_POST){
            $data = I('post.');
            $data['bute_val'] = str_replace("\r\n",',',$data['bute_val']);
            if($m->where(array('id'=>$id))->save($data)){
                $this->success('恭喜，属性修改成功！',U('attributeList',array('id'=>$data['category_id'])));
            } else {
                $this->error('修改失败！');
            }
        }else{
            $currentmenu = $m->where(array('id'=>$id))->find();
         
            $currentmenu['bute_val'] = str_replace(',',"\r\n",$currentmenu['bute_val']);
            $option = M('goods_type')->select();
            $option = $this->getMenu($option);
            $this->assign('currentmenu',$currentmenu);
            $this->assign('option',$option);
            $this->display('attributeForm');
        }
    }
    public function attributeDel(){
    
        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        //uid为1的禁止删除
        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
        }
        $map['id'] = array('in', $ids);
       
        if (M('attribute')->where($map)->delete()) {
            addlog('删除属性ID：' . $ids);
            $this->success('恭喜，属性删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }
} 